﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 交易员五日均值
    /// </summary>
    public class OperatorFiveDayAvg
    {
        public int Id { get; set; }

        /// <summary>
        /// 用户编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }

        //public void InitOperatorFiveDayAvg(int tradeHistoryId, DB.DBManager db)
        //{
        //    List<BenefitHelper.Sys.Operator> operators = db.Operators.ToList();
        //    foreach (BenefitHelper.Sys.Operator op in operators)
        //    {
        //        SetOperatorFiveDayAvg(op.Id, tradeHistoryId, db);
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = db.OperatorFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
        //    foreach (BenefitHelper.View.OperatorFiveDayAvg avg in query)
        //    {
        //        db.OperatorFiveDayAvg.Remove(avg);
        //    }
        //    db.SaveChanges();
        //}
        ///// <summary>
        ///// 获取交易员查询日期之前的五日均值
        ///// </summary>
        ///// <param name="operatorId"></param>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public OperatorFiveDayAvg GetOperatorFiveDayAvg(int operatorId, int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = db.OperatorFiveDayAvg.Where(a => a.OperatorId == operatorId).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
        //    if (query.Count > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return new OperatorFiveDayAvg();
        //    }
        //}


        ///// <summary>
        ///// 初始化交易员五日均值(这个方法应该没问题)
        ///// </summary>
        ///// <returns></returns>
        //public void SetOperatorFiveDayAvg(int operatorId, int tradeHistoryId, DB.DBManager db)
        //{
        //    OperatorFiveDayAvg change = new OperatorFiveDayAvg();
        //    change.OperatorId = operatorId;
        //    change.TradeHistoryId = tradeHistoryId;
        //    List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetPdateFromTradeHistoryId(tradeHistoryId, db), operatorId, db);
        //    if (accounts.Count > 0)
        //    {
        //        foreach (Sys.Account acc in accounts)
        //        {
        //            var queryChange = db.AccountFiveDayAvg.Where(a => a.TradeHistoryId == tradeHistoryId).Where(a => a.AccountId == acc.Id);
        //            if (queryChange.Count() > 0)
        //            {
        //                change.BillCount += queryChange.First().BillCount;
        //                change.DayCount += queryChange.First().DayCount;
        //                change.Free += queryChange.First().Free;
        //                change.LoseCount += queryChange.First().LoseCount;
        //                change.OperatorId = operatorId;
        //                change.Profit += queryChange.First().Profit;
        //                change.SumLost += queryChange.First().SumLost;
        //                change.SumWin += queryChange.First().SumWin;
        //                change.TradeHistoryId = tradeHistoryId;
        //                change.UsedMargin += queryChange.First().UsedMargin;
        //                change.WinCount += queryChange.First().WinCount;
        //                change.AvgBillBenefit += queryChange.First().AvgBillBenefit;
        //                change.AvgPostionTime += queryChange.First().AvgPostionTime;
        //                change.MaxReturn += queryChange.First().MaxReturn;
        //                change.OverStopLoss += queryChange.First().OverStopLoss;
        //                change.PingJinPing += queryChange.First().PingJinPing;
        //                change.RaiseRate += queryChange.First().RaiseRate;
        //                change.RiskCount += queryChange.First().RiskCount;
        //                change.Utilization += queryChange.First().Utilization;
        //                change.WinRate += queryChange.First().WinRate;
        //                change.Yield += queryChange.First().Yield;

        //            }
        //        }
        //        change.BillCount = Math.Round(change.BillCount / accounts.Count(), 2);
        //        change.DayCount = Math.Round(change.DayCount / accounts.Count(), 2);
        //        change.Free = Math.Round(change.Free / accounts.Count(), 2);
        //        change.LoseCount = Convert.ToInt32(change.LoseCount / accounts.Count());
        //        change.Profit = Math.Round(change.Profit / accounts.Count(), 2);
        //        change.SumLost = Math.Round(change.SumLost / accounts.Count(), 2);
        //        change.SumWin = Math.Round(change.SumWin / accounts.Count(), 2);
        //        change.UsedMargin = Math.Round(change.UsedMargin / accounts.Count(), 2);
        //        change.WinCount = Convert.ToInt32(change.WinCount / accounts.Count());
        //        change.AvgBillBenefit = Math.Round(change.AvgBillBenefit / accounts.Count(), 2);
        //        change.AvgPostionTime = Math.Round(change.AvgPostionTime / accounts.Count(), 2);

        //        change.OverStopLoss = Math.Round(change.OverStopLoss / accounts.Count(), 2);
        //        change.PingJinPing = Convert.ToInt32(change.PingJinPing / accounts.Count());
        //        change.RaiseRate = Math.Round(change.RaiseRate / accounts.Count(), 2);
        //        change.RiskCount = Convert.ToInt32(change.RiskCount / accounts.Count());
        //        change.Utilization = Math.Round(change.Utilization / accounts.Count(), 2);
        //        change.WinRate = Math.Round(change.WinRate / accounts.Count(), 2);
        //        change.Yield = Math.Round(change.Yield / accounts.Count(), 2);


        //    }

        //    db.OperatorFiveDayAvg.Add(change);


        //}
    }
}
